/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.data.controller.ProcurementLnCtlr;
import org.gov.vis.data.controller.ProcurementLnCtlrImpl;
import org.gov.vis.data.dto.ProcurementLNDto;
import org.gov.vis.data.gui.EditProcurementLNDialog;
import org.gov.vis.data.gui.ViewProcurementLNDialog;
import org.gov.vis.gui.VISDataGrid;
import org.gov.vis.main.Main;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class ViewProcurementLNListener implements ActionListener,BaseListener {


	


	
	private ProcurementLnCtlr procurementLNCtlr = new ProcurementLnCtlrImpl();
	private ViewProcurementLNDialog viewProcurementLNDialog;
	private ProcurementLNDto distributionLNDto;

	final private ResourceBundle rb = Util.getResources();
	private EditProcurementLNDialog editProcurementLNDialog;

	public ViewProcurementLNListener(ViewProcurementLNDialog viewProcurementLNDialog) {
		// TODO Auto-generated constructor stub
		this.viewProcurementLNDialog = viewProcurementLNDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub
		ViewProcurementLNDialog _viewProcurementLNDialog =(ViewProcurementLNDialog)_guiObject;
		distributionLNDto = new ProcurementLNDto();
		
		String code = _viewProcurementLNDialog.getCodeTxtBx().getText();
		if(code!=null){
		 distributionLNDto.setCode(code);
		}
		/*otherDto.setNameOfOrg(_othersDialog.getTxtFldOrg().getText());
		otherDto.setNameOfAIWorker(_othersDialog.getTxtFldAIWorker().getText());
		otherDto.setHeadQuarter(_othersDialog.getTxtFldHeadQuarter().getText());
		otherDto.setJuridiction(_othersDialog.getTxtFldJurisdiction().getText());
		otherDto.setMandal(_othersDialog.getTxtFldMandal().getText());
		otherDto.setDistrict(_othersDialog.getTxtFldDistrict().getText());*/
	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub
		final ProcurementLNDto _procurementLNDto = (ProcurementLNDto)_dtoObject;
		if(editProcurementLNDialog != null){
			editProcurementLNDialog.getTxtFldCode().setText(_procurementLNDto.getCode());
			editProcurementLNDialog.getTxtFldLNConvertedLtrs().setText(_procurementLNDto.getCnvrtLtrs());
			editProcurementLNDialog.getDtChoDateOfReceipt().setDate(_procurementLNDto.getDoRpt());
			editProcurementLNDialog.getTxtFldLNFromStation().setText(_procurementLNDto.getFrmStation());
			editProcurementLNDialog.getTxtFldLNKgs().setText(_procurementLNDto.getKgs());
			editProcurementLNDialog.getTxtFldLNVehicalNo().setText(_procurementLNDto.getVehicleNo());
			editProcurementLNDialog.getTxtFldLNEvoporationLoss().setText(_procurementLNDto.getEvoprLoss());
			editProcurementLNDialog.setHidnObject(_procurementLNDto.getPrLnId());
		}
	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		
		
		if(actionEvent.getActionCommand().equals("Search")){
			setGuiDetailsToDto(viewProcurementLNDialog);
			try {
				
			List<ProcurementLNDto> distributionLNLst = procurementLNCtlr.searchPrLNs(distributionLNDto);
			
			List<String> colLst = new ArrayList<String>();
			colLst.add("Code");
			colLst.add("Date of Receipt");
			//colLst.add("Kgs");
			colLst.add("From Station");
			colLst.add("Vehicle No");
			
			ArrayList<Object> dataList = new ArrayList<Object>(1);
			if(distributionLNLst != null && !distributionLNLst.isEmpty()){
				for(ProcurementLNDto dto:distributionLNLst){
					ArrayList<Object> lst = new ArrayList<Object>(1);
					//setting each property of the current Accessory object (row) to the list
					lst.add(dto);
					lst.add(dto.getCode());
					lst.add(dto.getDoRpt());
				//	lst.add(dto.getKgs());
					lst.add(dto.getFrmStation());
					lst.add(dto.getVehicleNo());
					dataList.add(lst);
				}//end for 
			}//end if
			VISDataGrid.createDataPanel(dataList, colLst, viewProcurementLNDialog.getPnlProcurementLNResults(),this);
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if(actionEvent.getSource() instanceof VISHyperLink){
			actionEvent.getSource();
			VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
			ProcurementLNDto distributionLNDto = (ProcurementLNDto)visHyperLink.getObject();
			//System.out.println("action from the object----"+distributionLNDto.getCode());
			editProcurementLNDialog = new EditProcurementLNDialog(Main.getMainFrame(),
                    rb.getString("dialog_edit_prmt_ln_title"),
                    false);
			setDtoDetailsToGui(distributionLNDto);
			viewProcurementLNDialog.dispose();

		}
		
	}






}
